import baseApi from './baseApi';
import { getMerchantId } from './merchantService';

// 响应接口类型
export interface ApiResponse<T> {
  code: number;
  message: string;
  data: T | null;
}

// 餐桌接口
export interface Table {
  id: number;
  name: string;
  tableNo: string;
  areaId: number;
  areaName: string;
  area: string;
  seats: number;
  status: string;
  processingTrade: number | null;
}

// 餐桌列表响应
export interface TableListResponse {
  list: Table[];
  total: number;
  page: number;
  pageSize: number;
}

// 分页请求参数
export interface PaginationParams {
  page?: number;
  pageSize?: number;
}

// 餐桌列表请求参数
export interface TableListParams extends PaginationParams {
  areaId?: number;
  status?: string;
  search?: string;
}

// 创建餐桌参数
export interface CreateTableParams {
  name: string;
  areaId: number;
  seats: number;
}

// 更新餐桌参数
export interface UpdateTableParams {
  name?: string;
  areaId?: number;
  seats?: number;
  status?: string;
}

/**
 * 获取餐厅当前商户ID
 */
export const getRestaurantId = async (): Promise<number> => {
  const merchantId = await getMerchantId();
  if (!merchantId) {
    throw new Error('未选择商户');
  }
  return merchantId;
};

// 餐桌服务
const tablesService = {
  /**
   * 获取餐桌列表
   * @param params 查询参数
   * @returns 餐桌列表响应
   */
  getTableList: async (params: TableListParams = {}): Promise<ApiResponse<TableListResponse>> => {
    try {
      const restaurantId = await getRestaurantId();
      const response = await baseApi.get(`/admin/restaurant/${restaurantId}/tables`, { params });
      return response.data;
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    } catch (error) {
      return {
        code: -1,
        message: '获取餐桌列表失败',
        data: null
      };
    }
  },

  /**
   * 获取餐桌详情
   * @param tableId 餐桌ID
   * @returns 餐桌详情
   */
  getTableDetail: async (tableId: number): Promise<ApiResponse<Table>> => {
    try {
      const response = await baseApi.get(`/admin/restaurant/tables/${tableId}`);
      return response.data;
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    } catch (error) {
      return {
        code: -1,
        message: '获取餐桌详情失败',
        data: null
      };
    }
  },

  /**
   * 创建餐桌
   * @param tableData 餐桌数据
   * @returns 创建结果
   */
  createTable: async (tableData: CreateTableParams): Promise<ApiResponse<Table>> => {
    try {
      const restaurantId = await getRestaurantId();
      const response = await baseApi.post(`/admin/restaurant/${restaurantId}/tables`, tableData);
      return response.data;
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    } catch (error) {
      return {
        code: -1,
        message: '创建餐桌失败',
        data: null
      };
    }
  },

  /**
   * 更新餐桌
   * @param tableId 餐桌ID
   * @param tableData 餐桌数据
   * @returns 更新结果
   */
  updateTable: async (tableId: number, tableData: UpdateTableParams): Promise<ApiResponse<Table>> => {
    try {
      const response = await baseApi.put(`/admin/restaurant/tables/${tableId}`, tableData);
      return response.data;
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    } catch (error) {
      return {
        code: -1,
        message: '更新餐桌失败',
        data: null
      };
    }
  },

  /**
   * 删除餐桌
   * @param tableId 餐桌ID
   * @returns 删除结果
   */
  deleteTable: async (tableId: number): Promise<ApiResponse<null>> => {
    try {
      const response = await baseApi.delete(`/admin/restaurant/tables/${tableId}`);
      return response.data;
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    } catch (error) {
      return {
        code: -1,
        message: '删除餐桌失败',
        data: null
      };
    }
  }
};

export default tablesService; 